home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / X / fonts / glyph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.3 KB  |  203 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * glyph.c:
  19.  *
  20.  *   a simple example of changing a window's cursor using "Glyph" cursors.  
  21.  *   A "Glyph" is just a character from one of the many text fonts that X 
  22.  *   knows about.  The size of your new glyph cursor is determined by the 
  23.  *   pixel size in the font's name.  This example should probably call 
  24.  *   `XQueryBestCursor()' to find the "best" (or at least the biggest 
  25.  *   usable) cursor size, but it simply arbitrarily chooses an Helvetica 
  26.  *   font with a pixel size of 25.  Note that some of the "character 
  27.  *   positions" in the array defining the characters in a font may be 
  28.  *   undefined, and among other things, a cursor must have non-zero width. 
  29.  *   So, after you load the font you've chosen, you need to query the "text 
  30.  *   extents" of each of the characters that might be defined in the font.  
  31.  *   Also note that the first glyph cursor "displayed" by the program is 
  32.  *   the space character which has a width, but doesn't display a very 
  33.  *   interesting cursor.
  34.  */
  35. #include <stdio.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/Xutil.h>
  38.  
  39. main()
  40. {
  41.   Display *dpy;
  42.   Window wind;
  43.   unsigned int scr;
  44.   unsigned int keep_going = 1;
  45.   XEvent ev;
  46.   unsigned int j, ichar, b1;
  47.   unsigned int i;
  48.   XColor backg, ebackg, foreg, eforeg, red, ered;
  49.   Window rwin;
  50.   Colormap dcm;
  51.   GC dgc;
  52.   const unsigned int MAX_glyphs=65535;
  53.   struct {
  54.     Cursor glyphc;
  55.     unsigned char byte1;
  56.     unsigned char byte2;
  57.   } curs[MAX_glyphs];
  58.   XFontStruct *fs;
  59.   unsigned char id[256];
  60.   static  unsigned char drexions[] = "Mouse button to cycle thru Glyph Cursors.";
  61.   static  unsigned char fontname[]
  62.     = "-adobe-helvetica-medium-r-normal--25-180-100-100-p-130-iso8859-1";
  63.   unsigned int num_glyphs;
  64.   XWMHints    wmhints;
  65.   XSizeHints  wmsizehints;
  66.   XClassHint  classhint;
  67.   
  68.   dpy = XOpenDisplay("");
  69.   scr = DefaultScreen(dpy);
  70.   rwin = RootWindow(dpy, scr);
  71.   dcm = DefaultColormap(dpy, scr);
  72.   dgc = DefaultGC(dpy,scr);
  73.  
  74. #if defined(DEBUGIT)
  75.   XSynchronize(dpy,True);
  76. #endif
  77.   if( !XAllocNamedColor(dpy, dcm, "darkslategray", &backg, &ebackg)) {
  78.     fprintf( stderr, "Error getting darkslategray\n" ); exit(1); }
  79.  
  80.   if( !XAllocNamedColor(dpy, dcm,"burlywood", &foreg, &eforeg)) {
  81.     fprintf( stderr, "Error getting burlywood\n" ); exit(1); }
  82.  
  83.   if( !XAllocNamedColor(dpy, dcm,"red", &red, &ered)) {
  84.     fprintf( stderr, "Error getting red\n" ); exit(1); }
  85.  
  86.   fs = XLoadQueryFont( dpy, fontname );
  87.   if( fs == NULL ) {
  88.     fprintf(stderr, "Couldn't find font %s\n", fontname);
  89.     exit(1);
  90.   }
  91.  
  92. #if defined(TRACEIT)
  93.   printf("Font Properties (names):\n");
  94.   for( i = 0; i < fs->n_properties; i++ ) {
  95.     printf("%s\n", XGetAtomName(dpy, fs->properties[i].name));
  96.   }
  97. #endif
  98.  
  99.   for( num_glyphs = 0, b1 = fs->min_byte1; b1 <= fs->max_byte1; b1++ ) {
  100.     for( ichar = 0;
  101.     (num_glyphs < MAX_glyphs) && (ichar <= fs->max_char_or_byte2);
  102.     ichar++ ) {
  103.       XChar2b thechar;
  104.       int direction, fontascent, fontdescent;
  105.       XCharStruct metrics;
  106. #if defined(TRACEIT)
  107.       printf("%d\n", ichar);
  108. #endif
  109.  
  110.       if( (fs->min_byte1 == 0) && (fs->max_byte1 == 0) ) {
  111.     thechar.byte1 = (ichar >> 8); /* high eight bits */
  112.     thechar.byte2 = (ichar & 255); /* low eight bits */
  113.       } else {
  114.     thechar.byte1 = (b1 & 255);
  115.     thechar.byte2 = (ichar & 255); /* low eight bits */
  116.       }
  117.  
  118.       XTextExtents16 (fs, &thechar, 1, &direction, &fontascent, &fontdescent,
  119.               &metrics);
  120. #if defined(TRACEIT)
  121.       printf( "char %d has width %d\n", ichar, metrics.width );
  122. #endif
  123.       if( metrics.width > 0 ) {
  124.     curs[num_glyphs].glyphc = XCreateGlyphCursor(dpy, fs->fid, fs->fid,
  125.                         ichar, ichar,
  126.                         &foreg, &red );
  127.     curs[num_glyphs].byte1 = b1;
  128.     curs[num_glyphs].byte2 = ichar;
  129.     if( curs[num_glyphs].glyphc == NULL ) {
  130.       fprintf( stderr, "Error creating cursor from font\n" );
  131.       exit(1);
  132.     }
  133.     num_glyphs += 1;
  134.       }
  135.     }
  136.   }
  137.   
  138.   wind = XCreateSimpleWindow( dpy, rwin, 0, 0, 512, 512, 2,
  139.                  foreg.pixel, backg.pixel);
  140.   classhint.res_name = "LEFT or RIGHT Mouse to Change Cursor";
  141.   classhint.res_class = "GlyphCursor";
  142.   XSetClassHint(dpy, wind, &classhint);
  143.  
  144.   wmhints.input = True;
  145.   wmhints.flags = InputHint;
  146.   XSetWMHints(dpy, wind, &wmhints);
  147.  
  148.   wmsizehints.x = 100;
  149.   wmsizehints.y = 100;
  150.   wmsizehints.width = 100;
  151.   wmsizehints.height = 100;
  152.   wmsizehints.flags = USPosition | USSize;
  153.   XSetWMNormalHints(dpy, wind, &wmsizehints);
  154.  
  155.   XSetWindowBackground( dpy, wind, backg.pixel );
  156.   XSetBackground( dpy, dgc, backg.pixel );
  157.   XSetForeground( dpy, dgc, foreg.pixel );
  158.  
  159.   XSelectInput( dpy, wind, ButtonPressMask|KeyPressMask|ExposureMask);
  160.   XMapWindow( dpy, wind );
  161.   
  162.   i = 0;
  163.   do { 
  164.     XNextEvent( dpy, &ev );
  165.     switch( ev.type )
  166.     {
  167.     case Expose:
  168.       while( XCheckTypedEvent(dpy, Expose, &ev) );
  169.       XUndefineCursor( dpy, wind );
  170.       XClearWindow(dpy, wind);
  171.       XDefineCursor( dpy, wind, curs[i%num_glyphs].glyphc );
  172.       sprintf(id, "(%u, %u)", curs[i%num_glyphs].byte1, curs[i%num_glyphs].byte2);
  173.       XDrawString(dpy, wind, dgc, 200, 200, id, strlen(id));
  174.       XDrawString(dpy, wind, dgc, 20, 20, drexions, strlen(drexions));
  175.       break;
  176.  
  177.     case KeyPress:
  178.       XCloseDisplay(dpy);
  179.       keep_going = 0;
  180.       break;
  181.  
  182.     case ButtonPress:
  183.       if( ev.xbutton.button == Button1 )
  184.     i += 1;
  185.       else if( ev.xbutton.button == Button3 )
  186.     i -= 1;
  187.       else
  188.     i = 0;
  189.       XUndefineCursor( dpy, wind );
  190.       XClearWindow(dpy, wind);
  191.       XDefineCursor( dpy, wind, curs[i%num_glyphs].glyphc );
  192.       sprintf(id, "(%u, %u)", curs[i%num_glyphs].byte1, curs[i%num_glyphs].byte2);
  193.       XDrawString(dpy, wind, dgc, 200, 200, id, strlen(id));
  194.       XDrawString(dpy, wind, dgc, 20, 20, drexions, strlen(drexions));
  195.       break;
  196.  
  197.     default:
  198.       break;
  199.     }
  200.   } while( keep_going );
  201.   exit(0);
  202. }
  203.